package cn.yunyichina.provider.sb.service.impl;

import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.sb.constant.Constant;
import cn.yunyichina.provider.sb.dao.MedicalPatientDao;
import cn.yunyichina.provider.sb.entity.MedicalPatient;
import cn.yunyichina.provider.sb.util.Util;
import cn.yunyichina.provider.sb.vo.MedicalPatientVo;
import cn.yunyichina.provider.sb.vo.SbRequest;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alipay.api.response.AlipayCommerceDataSendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by 15-AK on 2017/7/12.
 */
public class UploadPatientServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(UploadPatientServiceImpl.class);
    private MedicalPatientDao medicalPatientDao = SpringContextHandler.getBean("medicalPatientDao");

    public void executeUploadPatientMessage() {
        try {
            MedicalPatient queryMedicalPatient = new MedicalPatient();
            queryMedicalPatient.setOrgCode(Constant.HOSPITAL_CODE);
            List<MedicalPatient> medicalPatients = medicalPatientDao.selectUnuploadMedical(queryMedicalPatient);
            if (CollectionUtils.isNotEmpty(medicalPatients)) {
                logger.info("就诊人信息上传总条数medicalPatients ={}", medicalPatients.size());
                for (MedicalPatient unUploadMedical : medicalPatients) {
                    logger.info("patIdNo={},待上传就诊人信息 unUploadMedical ={}", new Object[]{unUploadMedical.getPatIdNo(),JsonUtils.toJsonString(unUploadMedical)});
                    MedicalPatientVo medicalPatientVo = new MedicalPatientVo();
                    SbRequest sbRequest = new SbRequest();
                    String encryptData = Util.encryptMedicalPatient(medicalPatientVo.buildMedicalPatientVo(unUploadMedical), Constant.ENCRYPT_KEY);
                    sbRequest.setSceneCode(Constant.MEDICAL_PATIENT);
                    sbRequest.setEncryptData(encryptData);
                    try {
                        AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                        logger.info("patIdNo={},就诊人信息上传支付宝响应参数 response ={}", new Object[]{unUploadMedical.getPatIdNo(),JsonUtils.toJsonString(response)});
                        if (response.isSuccess()) {
                            logger.info("patIdNo={},就诊人信息上传成功",unUploadMedical.getPatIdNo());
                            unUploadMedical.setUploadFlag(new Short("1"));
                        } else {
                            logger.info("patIdNo={},就诊人信息上传失败 errMsg={}", unUploadMedical.getPatIdNo(),response.getSubMsg());
                            unUploadMedical.setExceptionMessage(response.getSubMsg());
                        }
                        unUploadMedical.setUploadTimes(unUploadMedical.getUploadTimes() + 1);
                        medicalPatientDao.update(unUploadMedical);
                    } catch (Exception e) {
                        logger.error("就诊人信息上传系统异常", e);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("就诊人信息上传系统异常", e);
        }
    }
}
